package com.woyao.core;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.TextView;

import com.woyao.R;
import com.woyao.core.model.Area;
import com.woyao.core.model.AreaResponse;

import java.util.ArrayList;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import razerdp.basepopup.BasePopupWindow;

/**
 * Created by smellycat on 2016/8/21.
 */
public class AreaPopUp extends BasePopupWindow  {
    private int init_province;
    private int init_city;
    private int init_district;
    private View popupView;
    private TextView ok;
    private TextView cancel;
    private WheelView provinceView;
    private WheelView cityView;
    private WheelView districtView;
    private OnSuccessListener onSuccessListener;
    private Activity context;

    private ArrayList<Area>[] areaList;
    private boolean hasInit = false;



    public void setOnSuccessListener(OnSuccessListener onSuccessListener) {
        this.onSuccessListener = onSuccessListener;
    }

    public AreaPopUp(Activity context) {
        super(context);
        this.context = context;
        areaList = new ArrayList[3];
        ok = (TextView)findViewById(R.id.id_ok);
        cancel = (TextView)findViewById(R.id.id_cancel);
        provinceView = (WheelView)findViewById(R.id.id_province);
        cityView = (WheelView)findViewById(R.id.id_city);
        districtView = (WheelView)findViewById(R.id.id_district);

        provinceView.setVisibleItems(7);
        cityView.setVisibleItems(7);
        districtView.setVisibleItems(7);
//        paintArea(context, provinceView, new ArrayList<Area>(), 1);
//        paintArea(context, cityView, new ArrayList<Area>(), 2);
//        paintArea(context, districtView, new ArrayList<Area>(), 3);
        provinceView.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                //更新市
                UpdateCity(cityView, newValue);
            }
        });
        cityView.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                UpdateDistrict(districtView, newValue);
            }
        });
        districtView.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {

            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AreaPopUp.this.dismiss();
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int provinceIndex = provinceView.getCurrentItem();
                int cityIndex = cityView.getCurrentItem();
                int districtIndex = districtView.getCurrentItem();
                Area province_obj = null;
                Area city_obj = null;
                Area district_obj = null;
                String name = "";
                if (provinceIndex > 0 && province != null && province.size() > provinceIndex) {
                    name = province.get(provinceIndex).getName();
                    province_obj = province.get(provinceIndex);
                }
                if (cityIndex > 0 && city != null && city.size() > cityIndex) {
                    name += city.get(cityIndex).getName();
                    city_obj = city.get(cityIndex);
                }
                if (districtIndex > 0 && district != null && district.size() > districtIndex) {
                    name += district.get(districtIndex).getName();
                    district_obj = district.get(districtIndex);
                }
                if(AreaPopUp.this.onSuccessListener != null){
                    onSuccessListener.onSuccess(province_obj, city_obj, district_obj);
                }
                AreaPopUp.this.dismiss();
            }
        });
    }

    @Override
    protected Animation getShowAnimation() {
        return getTranslateAnimation(250*2,0,300);
    }

    @Override
    protected View getClickToDismissView() {
        return findViewById(R.id.click_to_dismiss);
    }

    @Override
    public View getPopupView() {
        popupView= LayoutInflater.from(mContext).inflate(R.layout.area_wheel,null);
        return popupView;
    }

    @Override
    public View getAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }

    public void init() {
        loadArea(context, provinceView, "0", 1);
    }

    public interface OnSuccessListener{
        void onSuccess(Area province, Area city, Area district);
    }

    private ArrayList<Area> province;
    private ArrayList<Area> city;
    private ArrayList<Area> district;

    private void paintArea(Context context, WheelView wheelView, ArrayList<Area> areas, int level) {
        Area none = new Area();
        none.setName("请选择");
        none.setId(-1);
        areas.add(0, none);
        switch (level) {
            case 1:
                province = areas;
                break;
            case 2:
                city = areas;
                break;
            case 3:
                district = areas;
                break;
        }
        String[] areaNames = new String[areas.size()];
        for (int index = 0; index < areas.size(); index++) {
            areaNames[index] = areas.get(index).getName();
        }
        areaList[level-1] = areas;
        wheelView.setViewAdapter(new ArrayWheelAdapter<String>(context, areaNames));
        wheelView.setCurrentItem(0);

        switch (level) {
            case 1:
                paintArea(context, cityView, new ArrayList<Area>(), 2);
                break;
            case 2:
                paintArea(context, districtView, new ArrayList<Area>(), 3);
                break;
            case 3:
                break;
        }

    }

    private int findIndex(ArrayList<Area> list, int id){
        for(int index = 0;index<list.size();index++){
            if(list.get(index).getId() == id){
                return index;
            }
        }
        return 0;
    }
    private void loadArea(final Context context, final WheelView wheelView, final String parentId, final int level) {

        AreaLoader loader = new AreaLoader(this.context, parentId);
        loader.setOnSuccessListener(new AreaLoader.OnSuccessListener() {
            @Override
            public void onSuccess(AreaResponse response) {

                if (response != null && response.getContent() != null) {
                    paintArea(context, wheelView, response.getContent(), level);
                    switch (level){
                        case 1:
                            if(AreaPopUp.this.init_province >0){

                                int index = findIndex(province, init_province);
                                if(index>0){
                                    wheelView.setCurrentItem(index);
                                }
                                init_province =0;
                            }
                            break;
                        case 2:
                            if(AreaPopUp.this.init_city >0){

                                int index = findIndex(city, init_city);
                                if(index>0){
                                    wheelView.setCurrentItem(index);
                                }
                                init_city =0;
                            }
                            break;
                        case 3:
                            if(AreaPopUp.this.init_district >0){

                                int index = findIndex(district, init_district);
                                if(index>0){
                                    wheelView.setCurrentItem(index);
                                }
                                init_district =0;
                            }
                            break;
                    }
                }
            }
        });
        loader.load();

    }

    private void UpdateCity(WheelView city, int index) {
        if (index == 0) {
            paintArea(context, city, new ArrayList<Area>(), 2);
        } else {
            if (province != null && province.size() > index) {
                Area parent = province.get(index);
                if (parent.getId() > 0) {
                    loadArea(context, city, parent.getId() + "", 2);
                }

            }
        }
    }

    private void UpdateDistrict(WheelView district, int index) {
        if (index == 0) {
            paintArea(context, district, new ArrayList<Area>(), 3);
        } else if (index > 0) {
            if (city != null && city.size() > index) {
                Area parent = city.get(index);
                if (parent.getId() > 0) {
                    loadArea(context, district, parent.getId() + "", 3);
                }

            }
        }
    }

    public void setInit_province(int init_province) {
        this.init_province = init_province;
    }

    public void setInit_city(int init_city) {
        this.init_city = init_city;
    }

    public void setInit_district(int init_district) {
        this.init_district = init_district;
    }
}
